package com.bsh.serviceImpl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.mybatis.model.ArticleWithBLOBs;
import com.bsh.repository.DaoArticle;
import com.bsh.service.ArticleService;

/**
 * 文章服务
 * 
 * @author gsh
 *
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
	@Autowired
	private DaoArticle artDao;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.ArticleService#getArticle(java.lang.Integer)
	 */
	@Override
	public ArticleWithBLOBs getArticle(Integer userid) {
		return artDao.selectByPrimaryKey(userid);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.ArticleService#findByTag(java.lang.String)
	 */
	@Override
	public ArticleWithBLOBs findByTag(String tag) {
		return artDao.findByTag(tag);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.ArticleService#saveArticle(com.bsh.mybatis.model.ArticleWithBLOBs)
	 */
	@Override
	public Integer saveArticle(ArticleWithBLOBs article) {
		String tag = article.getTags();
		ArticleWithBLOBs art = artDao.findByTag(tag);
		if (art == null) {
		    article.setCreateTime(new Date());
			return artDao.insert(article);
		}

		BeanCopier copy = BeanCopier.create(ArticleWithBLOBs.class, ArticleWithBLOBs.class, false);
		copy.copy(article, art, null);
		return artDao.updateByPrimaryKey(art);
	}

}
